<template>
  <el-card shadow="">
    <el-form :inline="true" :model="form" class="mb-4">
      <el-form-item label="模板标题">
        <el-input v-model="form.templateTitle" placeholder="请输入模板标题" clearable />
      </el-form-item>
      <el-form-item label="AgentId">
        <el-select v-model="form.agentId">
          <el-option v-for="item in AgentData" :key="item.id"  placeholder="请选择Agent" :label="item.title" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary" @click="openDialog()">新增分类</el-button>

    <el-table :data="tableData" style="width: 100%" border class="mt-4">
      <el-table-column prop="id" label="ID" width="60" />
      <el-table-column prop="templateTitle" label="素材名称" />  
      <el-table-column prop="coverUrl" label="封面图">
        <template #default="{ row }">
          <image-contain
              v-if="row.coverUrl"
              :src="row.coverUrl"
              :width="60"
              :height="45"
              :preview-src-list="[row.coverUrl]"
              preview-teleported
              fit="contain"
          />
        </template>
      </el-table-column>

      <!-- <el-table-column prop="software" label="软件" width="100" /> -->
      <el-table-column prop="format" label="格式" width="80" />
      <el-table-column prop="proportion" label="比例" width="80" />
      <el-table-column prop="size" label="大小 (MB)"  width="100"/>
      <el-table-column prop="pages" label="页数"  width="80"/>
      <el-table-column prop="viewOrder" label="展示顺序" width="100" />
      <el-table-column prop="limitation" label="限制" />
      <el-table-column prop="copyrightNotice" label="版权说明" />
      <el-table-column prop="templateIntroduce" label="素材简介" />
    
      <!-- <el-table-column prop="templateUrl" label="模板地址" /> -->



<!--      <el-table-column prop="parentId" label="父ID" width="80" />-->
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button type="primary" size="small" @click="submitForm(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="deleteItem(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :page-size="form.pageSize"
        :page-sizes="[5, 10, 20, 50]"
        :current-page="form.pageNum"
        :total="form.total"
        class="mt-4"
        @current-change="handlePageChange"
        @size-change="handleSizeChange"
    />

    <el-dialog :title="form.id ? '编辑分类' : '新增分类'" v-model="dialogVisible" width="50vw">
      <mediaDig  @submitForm="handleSubmit" :mediaDigData="mediaDigData" v-if="dialogVisible"></mediaDig>
    </el-dialog>
  </el-card>
</template>

<script setup>
import mediaDig from '../asset/mediaDig.vue'
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'
import { getTempList } from '@/apis/asset'
import {getList, templatedelete} from "../../apis/agent.js"; // 替换成你的真实接口路径

const tableData = ref([])
const mediaDigData = ref({})
const form = ref({
  pageSize: 10,
  pageNum: 1,
  total: 0,
  templateTitle: '',
  agentId: '',
  id: null,
  categoryName: '',
  parentId: 0,
  createTime: ''
})

// 弹窗开关
const dialogVisible = ref(false)
const AgentData = ref([])

const handleSubmit = () => {
  dialogVisible.value=false
  mediaDigData.value={}
  getLists()
}

const getListAgent =  () => {
  getList({pageNum:1,pageSize:1000}).then(res=>{
    AgentData.value=res.list
  })
    // AgentData.value=reson.list
}
const openDialog = (row = null) => {
  if (row) {
    form.value = { ...form.value, ...row } // 保留分页参数
  } else {
    form.value = {
      ...form.value,
      id: null,
      categoryName: '',
      parentId: 0,
      createTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
    }
  }
  dialogVisible.value = true
}

const getLists = () => {
  const params = {
    pageNum: form.value.pageNum,
    pageSize: form.value.pageSize,
    templateTitle: form.value.templateTitle,
    agentId: form.value.agentId
  }
  getTempList(params).then(res => {
    tableData.value = res.list || []
    form.value.total = res.total || 0
  }).catch(() => {
    ElMessage.error('获取数据失败')
  })
}

const submitForm = (item) => {
  mediaDigData.value=item
  dialogVisible.value = true
}

const deleteItem = (id) => {
  templatedelete({id:id}).then(()=>{
    ElMessage.success('删除成功')
    getLists()
  })
}

const handlePageChange = (page) => {
  form.value.pageNum = page
  getLists()
}

const handleSizeChange = (size) => {
  form.value.pageSize = size
  form.value.pageNum = 1
  getLists()
}

const handleSearch = () => {
  form.value.pageNum = 1
  getLists()
}

const resetForm = () => {
  form.value.templateTitle = ''
  form.value.agentId = ''
  form.value.pageNum = 1
  getLists()
}

onMounted(() => {

  getListAgent()
  getLists()
})
</script>
